package com.zhu.gmallpublisher.controller;

import com.zhu.gmallpublisher.bean.CouponReduceStats;
import com.zhu.gmallpublisher.service.CouponStatsService;
import com.zhu.gmallpublisher.utils.GetDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.List;

/**
 * 优惠券主题
 */
@RestController
@RequestMapping("/zhu/gmall/sugar/coupon")
public class CouponStatsController {

    @Autowired
    private CouponStatsService couponStatsService;

    @RequestMapping("/use")
    public String getCouponStats(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if( date == 0){
            date = GetDateUtil.getCurrentTime();
        }
        List<CouponReduceStats> couponStatsList = couponStatsService.getCouponStats(date);
        if(couponStatsList == null || couponStatsList.size() == 0){
            return "";
        }
        StringBuilder dataJson = new StringBuilder("[");
        for (int i = 0; i < couponStatsList.size(); i++) {
            CouponReduceStats couponReduceStats = couponStatsList.get(i);
            Double couponReduceAmount = couponReduceStats.getCouponReduceAmount();
            Double originalTotalAmount = couponReduceStats.getOriginalTotalAmount();
            Double couponSubsidyRate = couponReduceStats.getCouponSubsidyRate();
            dataJson.append("{\n" +
                    "        \"couponReduceAmount\": "+ couponReduceAmount +",\n" +
                    "        \"originTotalAmount\": "+ originalTotalAmount +",\n" +
                    "        \"couponSubsidyRate\": "+ couponSubsidyRate +"\n" +
                    "      }");
            if(i < couponStatsList.size() - 1){
                dataJson.append(",");
            }else {
                dataJson.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"优惠券减免金额\",\n" +
                "        \"id\": \"couponReduceAmount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"原始金额总和\",\n" +
                "        \"id\": \"originTotalAmount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"优惠券补贴率\",\n" +
                "        \"id\": \"couponSubsidyRate\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": "+ couponStatsService +"\n" +
                "  }\n" +
                "}";

    }
}
